//
//  ShardDialogViewController.swift
//  weile
//
//  Created by LKM on 16/11/8.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit
import Kingfisher


class ShardDialogViewController: BaseViewController {
    
    private struct Param {
        var  title :String?
        var  content:String?
        var url:String?
        var  image:UIImage?
    }
    
    private var mParam : Param=Param();
    
    @IBOutlet weak var img_temp_load: UIImageView!
    
    override func viewDidLoad() {
                super.viewDidLoad()
        img_temp_load.isHidden=true;
        
        isShowContentBg = false
        //透明
        self.view.backgroundColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
        self.modalPresentationStyle = .custom
        
        //没动画效果
        self.view.alpha = 0;
        Handler.post {
            UIView.animate(withDuration: 1, animations: {
                self.view.alpha = 1
            })
        }
        
        
    }
    
    
    /**
     localImage 当没有imageUrl的时候  或是imageUrl下载失败的时候 就会使用他
     url 一定不能为空 否则分享不出去
     */
    func binData(title:String?,content:String?,url:String?,imageUrl:String?,localImage:UIImage?){
        LKMLog("===\(title) \(content)  \(url)  \(imageUrl)")
        
        mParam.title=title;
        mParam.content=content;
        if(StringHelp.size(str: mParam.content)>80){
            mParam.content=StringHelp.substring(str: mParam.content, sIndex: 0, length: 80);
        }
        mParam.url=url;
        
        mParam.image=localImage;
        if(!StringHelp.isEmpty(imageUrl)){
            img_temp_load.kf.setImage(with: URL(string: imageUrl! ), placeholder: nil, options: nil, progressBlock: nil) { (image, error, cacheType, imageURL) in
                if error == nil && image != nil{
                    //生成缩略图
                    var w :CGFloat = 0
                    var h :CGFloat = 0//min( 100/(image?.size.width/(image?.size.height)!), 100*(image?.size.width/(image?.size.height)!) )
                    
//                    LKMLog("===iw ih \(image!.size.width)") 图片居然没压缩？还是很大
                    
                    if(image!.size.width > 200 || image!.size.height > 200){
                        if(image!.size.width>image!.size.height){
                            w = 200;
                            h = w * (image!.size.height/(image!.size.width))
                        }else {
                            h = 200;
                            w = h * (image!.size.width/(image!.size.height))
                        }
                    }
                    
                    
                    UIGraphicsBeginImageContext(CGSize(width: w, height:h ))
                    image?.draw(in: CGRect(x: 0, y: 0, width: w, height: h) )//(0, 0, 100, 100))
                    let thumbImage=UIGraphicsGetImageFromCurrentImageContext()
                    UIGraphicsEndImageContext()
                    
                    
                    self.mParam.image=thumbImage;
                }
            }
        }
    }

    @IBAction func onBgClick(_ sender: AnyObject) {
        UIView.animate(withDuration: kAnimationDuration, animations: {
            self.view.alpha = 0.1
        }) { (b:Bool) in
            self.finish(animated: false)
        }
        
    }

    @IBAction func onSharaWXClick(_ sender: AnyObject) {

        sendToWX(isWXSceneTimeline: false)
    }
    @IBAction func onShardisWXTimelineClick(_ sender: AnyObject) {
         sendToWX(isWXSceneTimeline: true)
    }
    
    func sendToWX(isWXSceneTimeline:Bool){
        //        ViewHelp.showTipsView(containView: self.view, tips: "xxx")
        //        分享到微信
        let wxm = WXMediaMessage.init()
        wxm.title=mParam.title;
        wxm.description=mParam.content
        
        wxm.setThumbImage(mParam.image);
        
        //        if(!StringHelp.isEmpty(mParam.url)){//一定要有url 不然分享不出去
        let wxWeb: WXWebpageObject  = WXWebpageObject.init()
        wxWeb.webpageUrl=mParam.url//"http://baidu.com"
        wxm.mediaObject=wxWeb;
        //        }
        
        let req=SendMessageToWXReq.init()
        req.bText=false;
        req.message=wxm;
        req.scene = Int32(isWXSceneTimeline ? WXSceneTimeline.rawValue : WXSceneSession.rawValue);
        
        WXApi.send(req)
        
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
//    override func getContentWrapView() -> UIView {
//        return contentWrapView
//    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
class ShardDialogView :UIView{
    
//    var mShardDialogViewController:ShardDialogViewController!
//    @IBAction func onBgClick(_ sender: AnyObject) {
//        ViewHelp.showTipsView(containView: mShardDialogViewController.view, tips: "ddd")
//        mShardDialogViewController.dismiss(animated: true, completion: nil);
//    }
    
    
}
